$(function(){
	//获取venue code值
	$.post("selectCode.action",function(data){
    	$("#venue").append(data);
    	$("#s_venue").append(data);
    });
	//输入限制（数字）
	$("#quantity,#s_quantity").numeric({allow:"."});
	//输入提示
	$("#quantity").poshytip({
		content: 'Enter a number',
		className: 'tip-yellowsimple',
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'inner-left',
		offsetX: 0,
		offsetY: 5
	});
	//动态加载商品名称
	$("#venue").change(function(){
		if($(this).val()=="")
		{
			$("#item option").remove();
		}
		$.post("selectVMItem.action",{venueCode:$("#venue").val()},function(data){
			$("#item option").remove();
			$("#item").append(data);
		});
	});
	
	//勾选全部
  	$("#selectAll").click(function(){
  		var checkBool=false;
  		if($(this).attr("checked"))
  			checkBool=true;
  		$("#form2 :checkbox").each(function(){
  			$(this).attr("checked",checkBool);
  		});
  	});
  	//删除全部
  	$("#deleteAll").click(function(){
  		$(".checkOne:checked").each(function(){
  			var aPos = oTable.fnGetPosition($(this).parent().parent().get(0));
  	  		oTable.fnDeleteRow(aPos);
  		});
  	});
  	//编辑所选列
  	var $tr;
  	$("#edit").live("click", function(){
  		$tr=$(this);
  		$.post("selectVMItem.action",{venueCode:$tr.parent().parent().find("td").eq(1).text()},function(data){
			$("#s_item option").remove();
			$("#s_item").append(data);
		});
  		$("#s_venue").val($tr.parent().parent().find("td").eq(1).text());
 		$("#s_date").val($tr.parent().parent().find("td").eq(2).text());
 		$("#s_item").val($tr.parent().parent().find("td").eq(3).text());
 		$("#s_quantity").val($tr.parent().parent().find("td").eq(4).text());
 		$("#s_reason").val($tr.parent().parent().find("td").eq(5).text());
  		$("#dialog-form1").dialog("open");
  	});
  	//删除所选列
  	$("#delete").live("click", function(){
  		var aPos = oTable.fnGetPosition($(this).parent().parent().get(0));
  		oTable.fnDeleteRow(aPos);
  	});
  	//添加信息到列表
  	var oTable;
	$("#submit").click(function(){
		var boolForm1=true;
		$("#item,#venue,#form1 input:not(#reason)").each(function(){
			if($(this).val()=="")
				boolForm1=false;
		});
		if(!boolForm1)
		{
			alert("All form with * are required");
			return;
		}
		$("#table2").append("<tr><td><input class='checkOne' type='checkbox'></input></td><td>"+
				$("#venue").val()+"</td><td>"+$("#date").val()+"</td><td>"+$("#item").val()+"</td><td>"
				+$("#quantity").val()+"</td><td>"+$("#reason").val()+"</td><td>"
				+"<a href='#' id='edit'>Edit</a>&nbsp&nbsp"+"<a href='#' id='delete'>Delete</a></td></tr>");
		$("#form2").show();
		oTable=$("#table2").dataTable({			
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"bProcessing":true,		
			"bDestroy":true,
			"sDom":'CT<"clear">R<"H"lfr>t<"F"ip>',
			"oTableTools":{
				"sSwfPath":"../../JavaScript/DataTables/extras/TableTools/media/swf/copy_cvs_xls_pdf.swf"
				},
			"oColVis": {
						"buttonText": "Change columns"
					}
		});	
	});	
	//保存信息到数据库
	$("#save").click(function(){
		var boolAdd=true;
		$("#table2 tr:not([id='title'])").each(function(){
			var trID=$(this).get(0);
			var venue = oTable.fnGetData(trID)[1];
			var date=oTable.fnGetData(trID)[2];
			var item=oTable.fnGetData(trID)[3];
			var quantity=oTable.fnGetData(trID)[4];
			var reason=oTable.fnGetData(trID)[5];
			$.post("saveDiscard.action",{
				venue:venue,
				date:date,
				item:item,
				quantity:quantity,
				reason:reason
			},function(data){
				if(data=="true")
				{				
			  		oTable.fnDeleteRow(trID);
				}
				else if(data="false")
				{
					boolAdd=false;
					alert("Add the Discard information failed");
					return;
				}
				else 
				{
					boolAdd=false;
					alert("Lost connection to the server");
					return;
				}
			});
		});
	});
	
	//编辑对话框
	$("#dialog-form1").dialog({
		autoOpen: false,
		height: 500,
		width: 500,
		modal: true,
		buttons: {
			"Save": function() {
				var boolForm1=true;
				$("#s_item,#s_venue,#form3 input:not(#s_reason)").each(function(){
					if($(this).val()=="")
						boolForm1=false;
				});
				if(!boolForm1)
				{
					alert("All form with * are required");
					return;
				}
				$tr.parent().parent().find("td").eq(1).html($("#s_venue").val());
				$tr.parent().parent().find("td").eq(2).html($("#s_date").val());
				$tr.parent().parent().find("td").eq(3).html($("#s_item").val());
				$tr.parent().parent().find("td").eq(4).html($("#s_quantity").val());
				$tr.parent().parent().find("td").eq(5).html($("#s_reason").val());
				$("#dialog-form1").dialog("close");
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
	});
});